package com.yidu.view;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.yidu.bean.TbImage;
import com.yidu.bean.TbMember;
import com.yidu.commom.Tool;
import com.yidu.message.Message;
import com.yidu.message.Pages;
import com.yidu.service.TbImageService;
import com.yidu.service.TbMemberService; 

/**
 * 类的描述-- 会员 action
 * @author 楠竹
 * @date 2018年7月12日
 * @version 1.0
 */
@Controller
@RequestMapping("/member")
public class TbMemberAction {

	//打印控制台
	Logger logger = Logger.getLogger(TbMemberAction.class);
	
	//消息信息类
	Message msg = new Message();
	 
	//json
	ObjectMapper mapper = new ObjectMapper();
	
	//会员
	@Resource
	private TbMemberService memberService;
	
	//图片
	@Resource
	private TbImageService imageService;
	
	@RequestMapping("/show")
	public String showMember(){
		logger.info("进入显示界面...");
		return "background/member/member";
	}
	
	/**
	 * 自行判断 增加 修改<br>
	 * 根据ID是否存在
	 * @param member
	 * @return
	 * @throws IOException 
	 */
	@RequestMapping(value="/saveOrUpdate",method=RequestMethod.POST)
	@ResponseBody
	public Message saveOrUpdate(HttpServletRequest request,TbMember member) throws IOException {
		
		logger.info("进入增加修改方法...");
		 
			logger.info(mapper.writeValueAsString(member));
		 
		 
		
		//判断是否存在id 
		int num = 0;
		 
		
		if(!Tool.isEmpty(member.getMemId())){
			//logger.info("");
			//存在 执行修改 
			num = memberService.update(member);
			 
		//	num = imageService.update(image);
			
		}else{
			//反之 执行增加
			//自行增加主键
			member.setMemId(Tool.getRandomString(32));
			num  =memberService.save(member);
			 
		}
	 
		//得到业务ID
		TbImage img = imageService.findById(member.getMemId()); 
		 
			logger.info(mapper.writeValueAsString(img));
		   
		 
		//判断是否为空
		if(!StringUtils.isEmpty(img)){
			//修改图片 
			img.setIamge(member.getFileName());
			img.setSort(1);
			num = imageService.update(img);
		}else{
			//增加 图片
			TbImage image = new TbImage();
			image.setYewuId(member.getMemId());
			image.setImgId(Tool.getRandomString(32));
			image.setIamge(member.getFileName());
			image.setSort(1);
			num = imageService.save(image);
		} 
		//根据 操作返回
		 msg.setNum(num);
		if(num>0){
			msg.setMsg("操作成功!");
		}else{
			msg.setMsg("操作失败!");
		}
		return msg; 
	}
	
	/**
	 *  接受前台 传递过来的参数<br>
	 *  执行删除方法
	 * @param memId
	 * @return
	 */
	@RequestMapping("/delete")
	@ResponseBody
	public  Object delete(String memId){

		 
			logger.info("进入删除方法....");
			int row = memberService.delete(memId);
			//HttpServletResponse response = ServletActionContext.getResponse();
			//response.setCharacterEncoding("utf-8");
			if(row>0){
				msg.setMsg("删除成功!");
			}else{ 
				msg.setMsg("删除失败!");
			} 
			 return msg;
	}
	
	
	@RequestMapping("/findVague")
	@ResponseBody
	public Object findVague(HttpServletRequest request,Pages pages,TbMember member) throws IOException{
		logger.info("进入模糊查询...");
		 
		
		logger.info(mapper.writeValueAsString(pages));
		//得到查询结果集
		List<TbMember> list = memberService.findVague(pages, member);
		
		 for (int i = 0; i < list.size(); i++) {
			TbMember members = list.get(i);
			
			//得到业务ID 查询出图片
			TbImage img = imageService.findById(members.getMemId());
			
			//判断是否为空  
			if(!StringUtils.isEmpty(img)){
				members.setImgId(img.getImgId());
				members.setFileName("../../../"+img.getIamge());
			}
			
			
			list.set(i, members);
			
		}
		logger.info(mapper.writeValueAsString(list));
		
		//类型转换
		Long lon= memberService.findCount(member);
		int row = lon.intValue();
		
				
		//由于前端所需条件
		Map<String, Object> map = new HashMap<>();
		
		map.put("code", 0);
		map.put("msg","");
		map.put("count",row);
		map.put("data", list);
		return map;
	}
	
	/**
	 * 根据ID查询对象类型
	 * 
	 * @param id
	 * @param response
	 * @return
	 */
	@RequestMapping("/showUpdate")
	@ResponseBody
	public Object showUpdate(String memId){
		logger.info("进入根据ID查询...");
		TbMember member = memberService.findById(memId);
		return member;
		
		
		  
	}
	/**
	 *  接受前台 传递过来的参数<br>
	 *  执行根据名字查询
	 * @param memId
	 * @return
	 */
	@RequestMapping("/findByName")
	@ResponseBody
	public  Message findByName(TbMember memName){

		 
			logger.info("进入根据查询名字....");
			List<TbMember> list= memberService.findByName(memName);
			if(list.isEmpty()){
				msg.setMsg("0");
				return msg;
			}else{ 
				msg.setMsg("1");
				return msg;
			} 
	}
	/**
	 *  接受前台 传递过来的参数<br>
	 *  执行登录判断
	 * @param memId
	 * @return
	 */
	@RequestMapping("/isLogin")
	@ResponseBody
	public  Message isLogin(TbMember mem,HttpSession session){
			
			logger.info("进入根据查询名字....");
			mem = memberService.findMemName(mem.getMemName());
			System.out.println("用户Id是"+mem.getMemId());
			int rows= memberService.islogin(mem);
			
			session.setAttribute("user", mem);
			//System.out.println("111111111111"+session.getAttribute("user"));
			if(rows>0){
				msg.setMsg("1");
				return msg;
			}else{ 
				msg.setMsg("0");
				return msg;
			} 
	}
	
	/**
	 * 得到登录的用户
	 * @param session 会话状态
	 * @return 存储在会话空间的用户
	 */
	@RequestMapping("/getUser")
	@ResponseBody
	public TbMember getUser(HttpSession session){
		TbMember member = (TbMember) session.getAttribute("user");
		return member;
	}
}
